package etnorservice.etnor_service.mapper.projectMapper;


import etnorservice.etnor_service.domain.DownloadToken;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DownloadTokenMapper {
    @Insert("INSERT INTO download_tokens(user_id, project_id, token, created_at, expires_at, is_used, ip_address) " +
            "VALUES(#{userId}, #{projectId}, #{token}, NOW(), #{expiresAt}, false, #{ipAddress})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DownloadToken token);

    @Select("SELECT * FROM download_tokens WHERE token = #{token} AND is_used = false AND expires_at > NOW()")
    DownloadToken findValidToken(String token);

    @Update("UPDATE download_tokens SET is_used = true, used_at = NOW() WHERE id = #{id}")
    int markAsUsed(Long id);
}